package flc.ast.activity;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.media.PlaybackParams;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.SeekBar;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.lxj.xpopup.core.BasePopupView;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityPlayBinding;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.TimeUtil;

public class PlayActivity extends BaseAc<ActivityPlayBinding> {

    public static String videoPath;
    private long videoLength;
    private Handler mHandler;
    private final Runnable mTaskUpdateTime = new Runnable() {
        @Override
        public void run() {
            mDataBinding.tvPlayLeftText.setText(TimeUtil.getMmss(mDataBinding.videoView.getCurrentPosition()));
            mDataBinding.sbPlay.setProgress(Integer.parseInt(TimeUtils.millis2String(mDataBinding.videoView.getCurrentPosition(), "ss")));
            mHandler.postDelayed(this, 500);
        }
    };
    private int speedKind = 0;
    private float selSpeed = 1.0f;

    @Override
    protected int onCreate() {
        return R.layout.activity_play;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPlayBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivPlay.setOnClickListener(this);
        mDataBinding.ivPlayStart.setOnClickListener(this);
        mDataBinding.tvPlaySpeed.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivPlayStart://暂停/播放
            case R.id.ivPlay:
                if (mDataBinding.videoView.isPlaying()) {
                    mDataBinding.ivPlayStart.setImageResource(R.drawable.zt);
                    mDataBinding.ivPlay.setImageResource(R.drawable.bf2);
                    mDataBinding.videoView.pause();
                    stopTime();
                } else {
                    mDataBinding.ivPlayStart.setImageResource(R.drawable.xbf);
                    mDataBinding.ivPlay.setImageResource(R.drawable.zt2);
                    mDataBinding.videoView.start();
                    startTime();
                }
                break;
            case R.id.tvPlaySpeed://变速
                if (speedKind == 0) {
                    speedKind = 1;
                    selSpeed = 2.0f;
                    mDataBinding.tvPlaySpeed.setText("2x");
                } else if (speedKind == 1) {
                    speedKind = 2;
                    selSpeed = 0.5f;
                    mDataBinding.tvPlaySpeed.setText("0.5x");
                } else if (speedKind == 2) {
                    speedKind = 0;
                    selSpeed = 1f;
                    mDataBinding.tvPlaySpeed.setText("1x");
                }
                setSpeed();
                break;
        }
    }

    @Override
    protected void initData() {
        mDataBinding.tvPlayTitle.setText(FileUtils.getFileNameNoExtension(videoPath));
        //设置播放器
        setPlayer();
    }

    //设置播放器
    private void setPlayer() {
        mHandler = new Handler();
        videoLength = MediaUtil.getDuration(videoPath);
        //设置seekbar
        mDataBinding.tvPlayLeftText.setText("00:00");
        mDataBinding.sbPlay.setMax(Integer.parseInt(TimeUtils.millis2String(videoLength, "ss")));
        mDataBinding.sbPlay.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mDataBinding.videoView.seekTo(seekBar.getProgress());
            }
        });
        //设置播放器
        mDataBinding.videoView.setVideoPath(videoPath);
        mDataBinding.videoView.seekTo(1);
        mDataBinding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {  // 播放完成的监听
                mDataBinding.tvPlayLeftText.setText("00:00");
                mDataBinding.ivPlayStart.setImageResource(R.drawable.zt);
                mDataBinding.ivPlay.setImageResource(R.drawable.bf2);
                mDataBinding.sbPlay.setProgress(0);
                mp.seekTo(1);
                stopTime();
            }
        });
        //开始播放视频
        mDataBinding.ivPlayStart.setImageResource(R.drawable.xbf);
        mDataBinding.ivPlay.setImageResource(R.drawable.zt2);
        mDataBinding.videoView.start();
        startTime();
    }

    private void startTime() {
        mHandler.post(mTaskUpdateTime);
    }

    private void stopTime() {
        mHandler.removeCallbacks(mTaskUpdateTime);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mDataBinding.ivPlayStart.setImageResource(R.drawable.zt);
        mDataBinding.ivPlay.setImageResource(R.drawable.bf2);
        mDataBinding.videoView.pause();
        stopTime();
    }

    //设置播放速度
    private void setSpeed() {
        mDataBinding.videoView.setVideoPath(videoPath);
        mDataBinding.videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onPrepared(MediaPlayer mp) {
                PlaybackParams playbackParams = mp.getPlaybackParams();
                playbackParams.setSpeed(selSpeed);
                mp.setPlaybackParams(playbackParams);
                mDataBinding.ivPlayStart.setImageResource(R.drawable.xbf);
                mDataBinding.ivPlay.setImageResource(R.drawable.zt2);
                startTime();
            }
        });
    }

}